const languageMap = {
    // 当 language 被认定为是左边的文字时，翻译为 prettier 内的模块
    js: "babel",
    javascript: "babel",
    ts: "typescript",
    typescript: "typescript",
    less: "less",
    scss: "scss",
    html: "html",
    xml: "html",
    css: "css",
    flow: "flow",
    json: "json",
    markdown: "markdown",
    mdx: "mdx",
    md: "markdown",
    vue: "vue",
    angular: "angular",
    lwc: "lwc",
    yaml: "yaml",
    graphql: "graphql",
};
const cache = {};
import prettier from "https://cdn.jsdelivr.net/npm/prettier@2.3.2/esm/standalone.mjs";
export default {
    prettier,
    // force: 是否强制加载
    async loadLanguage(lang, force = false) {
        const parser = languageMap[lang.trim().toLowerCase()];
        if (parser) {
            if (cache[parser] && !force) {
                console.success(`prettier: ${parser}解释器已经加载过了`);
                return true;
            }
            const { default: plugin } = await import(
                `https://cdn.jsdelivr.net/npm/prettier@2.3.2/esm/parser-${parser}.mjs`
            );
            console.success("prettier", lang, "加载成功");
            cache[parser] = plugin;
            return true;
        } else {
            console.error("prettier: 没有", lang, "的解释器");
            return false;
        }
    },
    format(code, lang = "js", options = { printWidth: 200, tabWidth: 4 }) {
        const parser = languageMap[lang.trim().toLowerCase()];
        if (parser && cache.hasOwnProperty(parser)) {
            return prettier.format(code, {
                parser,
                plugins: [cache[parser]],
                options,
            });
        } else {
            console.error(
                "prettier: 没有加载",
                lang,
                "或者",
                parser,
                "的解释器",
                "请先 loadLanguage()"
            );
        }
    },
};
